package demo2;

import java.util.Arrays;
import java.util.Comparator;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: happyiu
 * Date: 2021-11-26
 * Time: 2:05
 */
class Student implements Comparable<Student>{
    public int age;
    public String name;
    public double score;

    public Student(int age, String name, double score) {
        this.age = age;
        this.name = name;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", score=" + score +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        /*if(this.age > o.age) {
            return 1;
        }else if(this.age == o.age) {
            return 0;
        }else {
            return -1;
        }*/

        return this.age-o.age;
    }
}


class AgeComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.age-o2.age;
    }
}

public class Test {
    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student(12, "bit", 98.9);
        students[1] = new Student(6, "abc", 88.9);
        students[2] = new Student(18, "zhangsan", 18.9);
        AgeComparator ageComparator = new AgeComparator();
        System.out.println(Arrays.toString(students));
        Arrays.sort(students,ageComparator);//默认是从小到大的排序
        System.out.println(Arrays.toString(students));

    }

}
